package com.ruoyi.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.PageInfoUtils;
import com.ruoyi.mes.utils.RepeatUtils;
import com.ruoyi.mes.vo.list.ProTecListVo;
import com.ruoyi.mes.vo.product.ProTecVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IProTecService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品工艺Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class ProTecServiceImpl implements IProTecService
{
    @Autowired
    private ProceMtrMapper proceMtrMapper;

    @Autowired
    private ProceInfoMapper proceInfoMapper;

    @Autowired
    private ProInfoServiceImpl proInfoService;

    @Autowired
    private ProceMtrServiceImpl proceMtrService;

    @Autowired
    private ProTecMapper proTecMapper;

    @Autowired
    private MtrInfoMapper mtrInfoMapper;

    @Autowired
    private DeviceInfoServiceImpl deviceInfoService;

    @Autowired
    private DeviceMapper deviceMapper;



    /**
     * 查询产品工艺
     *
     * @param id 产品工艺主键
     * @return 产品工艺
     */
    @Override
    public ProTec selectProTecById(Long id)
    {
        return proTecMapper.selectProTecById(id);
    }

    /**
     * 查询产品工艺列表
     *
     * @param proTec 产品工艺
     * @return 产品工艺
     */
    @Override
    public List<ProTec> selectProTecList(ProTec proTec)
    {
        return proTecMapper.selectProTecList(proTec);
    }

    /**
     * 新增产品工艺
     *
     * @param proTec 产品工艺
     * @return 结果
     */
    @Override
    public int insertProTec(ProTec proTec)
    {
        proTec.setCreateTime(DateUtils.getNowDate());
        return proTecMapper.insertProTec(proTec);
    }

    /**
     * 修改产品工艺
     *
     * @param proTec 产品工艺
     * @return 结果
     */
    @Override
    public int updateProTec(ProTec proTec)
    {
        proTec.setUpdateTime(DateUtils.getNowDate());
        return proTecMapper.updateProTec(proTec);
    }

    /**
     * 批量删除产品工艺
     *
     * @param ids 需要删除的产品工艺主键
     * @return 结果
     */
    @Override
    public int deleteProTecByIds(Long[] ids)
    {
        return proTecMapper.deleteProTecByIds(ids);
    }

    /**
     * 删除产品工艺信息
     *
     * @param id 产品工艺主键
     * @return 结果
     */
    @Override
    public int deleteProTecById(Long id)
    {
        return proTecMapper.deleteProTecById(id);
    }

    /**
     * 增-工艺
     * @param proTecVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult savetec(ProTecVo proTecVo) {
        //判断工艺编码重复
        if (RepeatUtils.isTecCodeRepeat(proTecMapper,proTecVo)){
            return AjaxResult.error("工艺编码重复!");
        }
        //判断产品是否已经绑定工艺
        ProInfo proInfo = proInfoService.selectProInfoById(proTecVo.getProId());
        if (proInfo.getTecStatus() == 1){
            return AjaxResult.error("产品已绑定工艺!");
        }



        for (ProceInfo proceInfo : proTecVo.getProceInfos()) {
            //插入原料
            List<ProceMtr> proceMtrs = proceInfo.getProceMtrs();
            for (ProceMtr proceMtr : proceMtrs) {
                ProceMtr mtr = new ProceMtr();
                BeanUtils.copyProperties(proceMtr,mtr);
                mtr.setTecCode(proTecVo.getTecCode());
                mtr.setProceId(proceInfo.getId());
                proceMtrService.insertProceMtr(mtr);
            }

            //插入设备
            List<DeviceInfo> deviceInfos = proceInfo.getDeviceInfos();
            for (DeviceInfo deviceInfo : deviceInfos) {
                //判断设备状态
                Device device = deviceMapper.getDeviceByCode(deviceInfo.getDeviceCode());
                if (device.getAvailableStatus() != 0){
                    return AjaxResult.error("设备故障中!");
                }
                DeviceInfo info = new DeviceInfo();
                BeanUtils.copyProperties(deviceInfo,info);
                info.setTecCode(proTecVo.getTecCode());
                info.setProceId(proceInfo.getId());
                deviceInfoService.insertDeviceInfo(info);
            }

            //插入工艺
            ProTec proTec = new ProTec();
            proTec.setProId(proTecVo.getProId());
            proTec.setProceSeque(proceInfo.getProceSeque());
            proTec.setTecCode(proTecVo.getTecCode());
            proTec.setProceId(proceInfo.getId());
            insertProTec(proTec);
        }

        proInfo.setTecStatus(1L);
        proInfoService.updateProInfo(proInfo);

        return AjaxResult.success("新增成功!");
    }

    /**
     * 改-工艺
     * @param proTecVo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult edittec(ProTecVo proTecVo) {
        //删除工艺及相关工序原料等
        deleteProTec(proTecVo.getTecCode());

        for (ProceInfo proceInfo : proTecVo.getProceInfos()) {
            //插入原料
            List<ProceMtr> proceMtrs = proceInfo.getProceMtrs();
            for (ProceMtr proceMtr : proceMtrs) {
                ProceMtr mtr = new ProceMtr();
                BeanUtils.copyProperties(proceMtr,mtr);
                mtr.setTecCode(proTecVo.getTecCode());
                mtr.setProceId(proceInfo.getId());
                mtr.setMtrId(proceMtr.getMtrId());
                proceMtrService.insertProceMtr(mtr);
            }

            //插入设备
            List<DeviceInfo> deviceInfos = proceInfo.getDeviceInfos();
            for (DeviceInfo deviceInfo : deviceInfos) {
                DeviceInfo info = new DeviceInfo();
                BeanUtils.copyProperties(deviceInfo,info);
                info.setTecCode(proTecVo.getTecCode());
                info.setProceId(proceInfo.getId());
                deviceInfoService.insertDeviceInfo(info);
            }

            //插入工艺
            ProTec proTec = new ProTec();
            proTec.setProId(proTecVo.getProId());
            proTec.setProceSeque(proceInfo.getProceSeque());
            proTec.setTecCode(proTecVo.getTecCode());
            proTec.setProceId(proceInfo.getId());
            proTec.setRemark(proTecVo.getRemark());
            insertProTec(proTec);
        }
        return AjaxResult.success("修改成功!");
    }

    /**
     * 删-工艺-工艺编码
     *
     * @param tecCode
     * @return
     */
    @Override
    @Transactional
    public int deleteProTec(String tecCode) {
        ProTec proTec = new ProTec();
        proTec.setTecCode(tecCode);
        List<ProTec> proTecs = selectProTecList(proTec);
        for (ProTec tec : proTecs) {
            //删除原料
            proceMtrService.deleteMtrsBtTecAndProce(tecCode,tec.getProceId());
            //删除设备
            deviceInfoService.deletDeviceInfoByTecAndProce(tecCode,tec.getProceId());
        }
        return proTecMapper.deleteProTec(tecCode);
    }

    /**
     * 查-工艺分页
     * @param proTecListVo
     * @return
     */
    @Override
    public List<ProTecListVo> selectProTecVoList(ProTecListVo proTecListVo) {
        List<ProTec> proTecs = proTecMapper.selectProTecVo(proTecListVo);

        List<ProTecListVo> tecListVos = new ArrayList<>();

        if (proTecs.size() != 0){
            for (ProTec proTec : proTecs) {
                ProTecListVo tecListVo = new ProTecListVo();
                ProInfo proInfo = proInfoService.selectProInfoById(proTec.getProId());
                tecListVo.setId(proTec.getId());
                tecListVo.setProName(proInfo.getProName());
                tecListVo.setProCode(proInfo.getProCode());
                tecListVo.setTecCode(proTec.getTecCode());
                tecListVo.setRemark(proTec.getRemark());
                tecListVos.add(tecListVo);
            }
        }

        return PageInfoUtils.toPageInfo(proTecs,tecListVos);
    }

    /**
     * 查 -工艺详情
     * @param id
     * @return
     */
    @Override
    public ProTecVo getDetals(Long id) {
        ProTecVo proTecVo = new ProTecVo();
        ProTec proTec = proTecMapper.selectProTecById(id);
        proTecVo.setProId(proTec.getProId());
        proTecVo.setTecCode(proTec.getTecCode());
        proTecVo.setId(proTec.getId());
        List<ProTec> proTecs = proTecMapper.selectProTecListByTecCode(proTec.getTecCode());
        List<ProceInfo> proceInfos = new ArrayList<>();

        for (ProTec tec : proTecs) {
            ProceInfo proceInfo = proceInfoMapper.selectProceInfoById(tec.getProceId());
            proceInfo.setId(tec.getProceId());
            proceInfo.setProceSeque(tec.getProceSeque());

            List<ProceMtr> mtrs = proceMtrMapper.getMtrByProceId(proTecVo.getTecCode(), tec.getProceId());
            for (ProceMtr mtr : mtrs) {
                mtr.setMtrCode( mtrInfoMapper.selectMtrInfoById(mtr.getMtrId()).getMtrCode());
            }
            proceInfo.setProceMtrs(mtrs);
            List<DeviceInfo> devices = deviceInfoService.getDevicesByProceIdAndTecCode(proTecVo.getTecCode(), tec.getProceId());
            proceInfo.setDeviceInfos(devices);

            proceInfos.add(proceInfo);
        }
        proTecVo.setProceInfos(proceInfos);
        return proTecVo;
    }


    /**
     * 查 - 工序 - 产品id相同
     * @param id
     * @return
     */
    @Override
    public List<ProTec> getProTecsByProId(Long id) {
        return proTecMapper.getProTecsByProId(id);
    }
}
